/**
 * 习题标签 - 主界面
 * 
 * @author 李静波
 */
Ext.define("PSI.Drill.DrillTagMainForm", {
	extend : "PSI.AFX.BaseOneGridMainForm",

	config : {},

	/**
	 * 重载父类方法
	 */
	afxGetToolbarCmp : function() {
		var me = this;

		var buttons = [ {
			text : "新增习题标签",
			handler : me.onAddTag,
			scope : me,
			iconCls : "PSI-button-add"
		}, "-", {
			text : "编辑习题标签",
			handler : me.onEditTag,
			scope : me,
			iconCls : "PSI-button-edit"
		}, "-", {
			text : "刷新",
			handler : me.onRefresh,
			scope : me,
			iconCls : "PSI-button-refresh"
		}, "-", {
			text : "关闭",
			iconCls : "PSI-button-exit",
			handler : function() {
				me.closeWindow();
			}
		} ];

		return buttons;
	},

	/**
	 * 重载父类方法
	 */
	afxGetMainGrid : function() {
		var me = this;
		if (me.__mainGrid) {
			return me.__mainGrid;
		}

		var modelName = "PSI_Drill_DrillTagMainForm_DrillTag";
		Ext.define(modelName, {
			extend : "Ext.data.Model",
			fields : [ "id", "name", "note", "showOrder" ],
			idProperty : "id"
		});
		var store = Ext.create("Ext.data.Store", {
			model : modelName,
			pageSize : 20,
			proxy : {
				type : "ajax",
				actionMethods : {
					read : "POST"
				},
				url : me.URL("Home/Drill/tagList"),
				reader : {
					root : 'dataList',
					totalProperty : 'totalCount'
				}
			},
			autoLoad : true
		});

		me.__mainGrid = Ext.create("Ext.grid.Panel", {
			viewConfig : {
				enableTextSelection : true
			},
			loadMask : true,
			border : 0,
			columnLines : true,
			columns : [ Ext.create("Ext.grid.RowNumberer", {
				text : "序号",
				width : 50
			}), {
				text : "标签",
				dataIndex : "name",
				width : 120,
				menuDisabled : true,
				sortable : false
			}, {
				text : "备注",
				dataIndex : "note",
				width : 300,
				menuDisabled : true,
				sortable : false
			}, {
				text : "显示次序",
				dataIndex : "showOrder",
				width : 60,
				menuDisabled : true,
				sortable : false
			} ],
			store : store,
			tbar : [
					{
						id : "pagingToobar",
						xtype : "pagingtoolbar",
						border : 0,
						store : store
					},
					"-",
					{
						xtype : "displayfield",
						value : "每页显示"
					},
					{
						id : "comboCountPerPage",
						xtype : "combobox",
						editable : false,
						width : 60,
						store : Ext.create("Ext.data.ArrayStore", {
							fields : [ "text" ],
							data : [ [ "20" ], [ "50" ], [ "100" ], [ "300" ],
									[ "1000" ] ]
						}),
						value : 20,
						listeners : {
							change : {
								fn : function() {
									store.pageSize = Ext.getCmp(
											"comboCountPerPage").getValue();
									store.currentPage = 1;
									Ext.getCmp("pagingToobar").doRefresh();
								},
								scope : me
							}
						}
					}, {
						xtype : "displayfield",
						value : "条记录"
					} ],
			bbar : {
				xtype : "pagingtoolbar",
				store : store
			}
		});

		return me.__mainGrid;
	},

	/**
	 * 刷新
	 */
	onRefresh : function() {
		Ext.getCmp("pagingToobar").doRefresh();
	},

	/**
	 * 新增习题标签
	 */
	onAddTag : function() {
		var me = this;

		var form = Ext.create("PSI.Drill.DrillTagEditForm", {
			parentForm : me
		});

		form.show();
	},

	/**
	 * 编辑习题标签
	 */
	onEditTag : function() {
		var me = this;
		var item = me.getMainGrid().getSelectionModel().getSelection();
		if (item == null || item.length != 1) {
			me.showInfo("请选择要编辑的习题标签");
			return;
		}

		var tag = item[0];

		var form = Ext.create("PSI.Drill.DrillTagEditForm", {
			parentForm : me,
			entity : tag
		});

		form.show();

	}
});